package com.taotao.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {
	@Value("REDIS_ITEM_KEY")
	private String REDIS_ITEM_KEY;

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	@Autowired
	private JedisClient jedisClient;

	@Override
	public TaotaoResult getItemInfoById(Long id) {
		// 查询缓存
		String key = id + ":info";
		try {
			String hget = jedisClient.hget(REDIS_ITEM_KEY, key);
			if (!StringUtils.isBlank(hget)) {
				return TaotaoResult.ok(hget);
			}
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}
		//数据库查询数据
		TbItem item = itemMapper.selectByPrimaryKey(id);

		if (item == null) {
			return TaotaoResult.build(400, "不存在id为:" + id + " 的商品");
		}
		// 添加缓存
		try {
			String json = JsonUtils.objectToJson(item);
			jedisClient.hset(REDIS_ITEM_KEY, key, json);
			return TaotaoResult.ok(item);
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}

	}

	@Override
	public TaotaoResult getItemDescById(Long id) {
		// 查询缓存
		String key = id + ":desc";
		try {
			String hget = jedisClient.hget(REDIS_ITEM_KEY, key);
			if (!StringUtils.isBlank(hget)) {
				return TaotaoResult.ok(hget);
			}
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}
		
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
		
		
		if (itemDesc == null) {
			return TaotaoResult.build(400, "不存在id为:" + id + " 的商品");
		}
		// 添加缓存
		try {
			String json = JsonUtils.objectToJson(itemDesc);
			jedisClient.hset(REDIS_ITEM_KEY, key, json);
			return TaotaoResult.ok(itemDesc);
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}

	}

	@Override
	public TaotaoResult getItemParamById(Long id) {
		
		// 查询缓存
		String key = id + ":param";
		try {
			String hget = jedisClient.hget(REDIS_ITEM_KEY, key);
			if (!StringUtils.isBlank(hget)) {
				return TaotaoResult.ok(hget);
			}
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}
		
		TbItemParamItemExample example = new TbItemParamItemExample();
		example.createCriteria().andItemIdEqualTo(id);
		List<TbItemParamItem> itemParams = itemParamItemMapper.selectByExampleWithBLOBs(example);
		
		if(itemParams==null || itemParams.isEmpty())
			return TaotaoResult.build(400, "不存在id为:" + id + " 的商品");
		
		TbItemParamItem  itemParam=itemParams.get(0);
		// 添加缓存
		try {
			String json = JsonUtils.objectToJson(itemParam);
			jedisClient.hset(REDIS_ITEM_KEY, key, json);
			return TaotaoResult.ok(itemParam);
		} catch (Exception e) {
			String stackTrace = ExceptionUtil.getStackTrace(e);
			return TaotaoResult.build(500, stackTrace);
		}

	}

}
